package com.fxm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fxm.dao.RecordMapper;
import com.fxm.domain.Record;
import com.fxm.returns.RespCode;
import com.fxm.returns.RespTable;
import com.fxm.service.RecordService;
import com.fxm.vo.PageVO;
import com.fxm.vo.result.RecordResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 答题记录 服务实现类
 * </p>
 *
 * @author fxm
 * @since 2021-02-04
 */
@Slf4j
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {

    @Override
    public RespTable<List<RecordResult>> listRecord(Integer page, Integer limit, String username) {
        Integer count = baseMapper.count(username);
        if (Objects.isNull(count) || count.equals(0)) {
            return new RespTable<>(RespCode.OK, new ArrayList<>(), 0);
        }

        PageVO pageVO = new PageVO(page, limit, count);
        List<RecordResult> recordResultList = baseMapper.listRecord(pageVO.getBegin() - 1, pageVO.getPageSize(), username);

        return new RespTable<>(RespCode.OK, recordResultList, pageVO.getCount());
    }
}
